(in-package :timeclock)

(define-application-frame timeclock ()
  ((employees :initarg :employees
	      :accessor employees))
  (:menu-bar menubar-command-table)
  (:panes
   (employees-pane :application
		   :display-function #'display-employees-pane))
  (:layouts
   (default employees-pane)))

(defmethod display-employees-pane ((frame timeclock) stream)
  (formatting-table (stream :x-spacing 50)
    (formatting-row (stream)
      (with-text-face (stream '(:bold :italic))
	(formatting-cell (stream) (write-string "Name" stream))
	(formatting-cell (stream) (write-string "Status" stream))
	(formatting-cell (stream) (write-string "t" stream))))
    (dolist (employee (employees frame))
      (present employee 'employee :stream stream))))

(define-timeclock-command (com-save-state :name t :menu "Save state") ()
  (with-open-file (file "records.tmp"
			:direction :output
			:if-exists :supersede)
    (s-serialization:serialize-xml (employees *application-frame*) file)))

(define-timeclock-command (com-load-state :name t :menu "Load state") ()
  (with-open-file (file "records.tmp"
			:direction :input
			:if-does-not-exist :error) ;FIXME: :error?
    (setf (employees *application-frame*)
	  (s-serialization:deserialize-xml file))))

(defparameter *employees*
  (list (make-instance 'employee :first-name "Jerry" :last-name "Nedry" :hourly-wage 650)
	(make-instance 'employee :first-name "Larry" :last-name "Stooge" :hourly-wage 350)
	(make-instance 'employee :first-name "Curly" :last-name "Scrooge" :hourly-wage 1205)
	(make-instance 'employee :first-name "Moe" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Joe" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Loe" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Goe" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Ioe" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Moe" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "343" :last-name "Guilty Spark" :hourly-wage 513)
	(make-instance 'employee :first-name "Illie" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Jim" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Bob" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Jim-Bob" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Hebie" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Kiln" :last-name "Deluge" :hourly-wage 513)
	(make-instance 'employee :first-name "Jake" :last-name "Deluge" :hourly-wage 513)))

(define-timeclock-command com-punch ((employee 'employee))
  (punch employee))

(define-timeclock-command com-inspect-time ((employee 'employee))
  (run-frame-top-level
   (make-application-frame 'time-inspector
			   :height 330
			   :width 520
			   :employee employee)))

(define-presentation-to-command-translator punch
    (employee com-punch timeclock
	  :gesture :select ;command activated with left-click on a node
	  :menu t)	    ;includes this command in right-click menu
  (object) (list object))

(define-presentation-to-command-translator inspect-time
    (employee com-inspect-time timeclock
	      :gesture :describe
	      :menu t)
  (object) (list object))

;; Menu bar
;;;;;;;;;;;

(define-timeclock-command (com-quit-frame :name t :menu "Quit")
   ()
  ;; FIXME: save state. And state should also be autoloaded.
  (frame-exit *application-frame*))

(define-command-table menubar-command-table
    :menu (("Save state" :command com-save-state)
	   ("Load state" :command com-load-state)
	   ("Quit" :command com-quit-frame)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;#+nil
(run-frame-top-level
 (make-application-frame 'timeclock
			 :height 330
			 :width 330
			 :employees *employees*))